<?php

namespace System;

/**
 * Description of Tidy
 *
 * @author nofear
 */
class Tidy {
    
    public static $_instance = null;
    
    private function __construct() {}
    
    public function html5($html, $options = null, $encoding = 'utf8') {
        if (extension_loaded('tidy') === true) {
            $default = array
                (
                'anchor-as-name' => false,
                'break-before-br' => true,
                'char-encoding' => $encoding,
                'decorate-inferred-ul' => false,
                'doctype' => 'omit',
                'drop-empty-paras' => false,
                'drop-font-tags' => true,
                'drop-proprietary-attributes' => false,
                'force-output' => false,
                'hide-comments' => false,
                'indent' => true,
                'indent-attributes' => false,
                'indent-spaces' => 2,
                'input-encoding' => $encoding,
                'join-styles' => false,
                'logical-emphasis' => false,
                'merge-divs' => false,
                'merge-spans' => false,
                'new-blocklevel-tags' => 'article aside audio details dialog figcaption figure footer header hgroup menutidy nav section source summary track video',
                'new-empty-tags' => 'command embed keygen source track wbr',
                'new-inline-tags' => 'canvas command data datalist embed keygen mark meter output progress time wbr',
                'newline' => 0,
                'numeric-entities' => false,
                'output-bom' => false,
                'output-encoding' => $encoding,
                'output-html' => true,
                'preserve-entities' => true,
                'quiet' => true,
                'quote-ampersand' => true,
                'quote-marks' => false,
                'repeated-attributes' => 1,
                'show-body-only' => true,
                'show-warnings' => false,
                'sort-attributes' => 1,
                'tab-size' => 4,
                'tidy-mark' => false,
                'vertical-space' => true,
                'wrap' => 0,
            );

            $doctype = $menu = null;

            if ((strncasecmp($html, '<!DOCTYPE', 9) === 0) || (strncasecmp($html, '<html', 5) === 0)) {
                $doctype = '<!DOCTYPE html>';
                $options['show-body-only'] = false;
            }

            $options = (is_array($options) === true) ? array_merge($default, $options) : $default;

            if (strpos($html, '<menu') !== false) {
                $menu = array
                    (
                    '<menu' => '<menutidy',
                    '</menu' => '</menutidy',
                );
            }

            if (isset($menu) === true) {
                $html = str_replace(array_keys($menu), $menu, $html);
            }

            $html = tidy_repair_string($html, $options, $encoding);

            if (empty($html) !== true) {
                if (isset($menu) === true) {
                    $html = str_replace($menu, array_keys($menu), $html);
                }

                if (isset($doctype) === true) {
                    $html = $doctype . "\n" . $html;
                }

                return $html;
            }
        }

        return $html;
    }
    /**
     * 
     * @return \System\Tidy
     */
    public static function getInstance() {
        if ( !self::$_instance ) {
            self::$_instance = new Tidy;
        }
        
        return self::$_instance;
    }
}
